//
//  InsuranceVIew.m
//  SZBank
//
//  Created by rimi on 16/7/14.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "InsuranceVIew.h"
#import "BJInsureButton.h"
@interface  InsuranceVIew ()<ButtonAndTableViewDelegate>
{
    CGFloat scrWidth;/**< 滚动视图宽度 */
    CGFloat scrHeight;/**< 滚动视图高度 */
    NSMutableArray *buttonDataArray;/**< 按钮数据源数组 */
}
@property (nonatomic)ButtonAndTableView *subClassfiy;
@property (nonatomic)NSMutableArray *subDataArray;
@end
@implementation InsuranceVIew

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        scrWidth = self.scrollView.frame.size.width;
        scrHeight = self.scrollView.frame.size.height;
        self.scrollView.contentSize = CGSizeMake(scrWidth, scrHeight * 1.8);
        
        //保险分类网络数据访问
        [NetWorkingDataManager getProtocolInsuranceTypeWithParam:@{@"time":@"2013-01-01 12:40:10"} success:^(id sender) {
            NSMutableArray *dataArray = [NSMutableArray array];
            
            for (NSDictionary *dict in sender[@"datalist"]) {
                if (((NSString *)dict[@"NAME"]).length == 3) {
                   
                    [dataArray addObject:dict[@"NAME"]];
                }
            }
            //添加视图
            ButtonAndTableView *classfiy = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(0, 0, frameWidth * 0.5, frameHeight* 0.05) dataSource:@{@"title":dataArray[0],@"list":dataArray} backImageName:@"理财_下拉框" selectImageName:@"理财_下拉框"];
            classfiy.delegate = self;
    
            [self addSubview:classfiy];
            [self addSubview:self.subClassfiy];
            
        } failure:^(id sender) {
              NSLog(@"f%@",sender);
        }];
//保险详情网络数据访问
        [NetWorkingDataManager getProtocolInsuranceWithParam:@{@"time":@"2013-01-01 12:40:10"} success:^(id sender) {
            //遍历请求的数据，将东吴保险公司数据取出
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *dict in sender[@"datalist"]) {
                if ([dict[@"COMPANY"][@"COM_NAME"] isEqualToString:@"东吴人寿保险股份有限公司"]) {
                    [array addObject:dict];
                }
            }
            
            
            buttonDataArray = [NSMutableArray array];
          
            for (NSDictionary *dict in array) {
                NSMutableArray *subButtonArray = [NSMutableArray array];
                [subButtonArray addObject:dict[@"THUMBNAILURL"]];
                [subButtonArray addObject:dict[@"NAME"]];
                [subButtonArray addObject:@"产品特色："];
                [subButtonArray addObject:[NSString stringWithFormat:@"适用人群：%@",dict[@"SUITPERSON"]]];
                [subButtonArray addObject:[NSString stringWithFormat:@"保险期限：%@",dict[@"TIMELIMIT"]]];
                [subButtonArray addObject:[NSString stringWithFormat:@"可选保障：%@",dict[@"ENSURE"]]];
                [subButtonArray addObject:[NSString stringWithFormat:@"最高保额：%@",dict[@"MAX"]]];
                [buttonDataArray addObject:subButtonArray];
            }
            [self addButtonsWithArray:buttonDataArray];

        } failure:^(id sender) {
            NSLog(@"失败%@",sender);
        }];
        
        
        
        
    }
    return self;
}
#pragma mark -- actions
- (void)addButtonsWithArray:(NSArray *)array{
    for (int i = 0; i < array.count; i ++) {
        BJInsureButton *button = [[BJInsureButton alloc]initWithFrame:CGRectMake(scrWidth * 0.05, scrHeight * 0.02 + scrHeight * 0.33 * i, scrWidth * 0.9, scrHeight * 0.3) withDataArray:array[i]];
        [self.scrollView addSubview:button];
    }
}
#pragma mark -- ButtonAndTableViewDelegate
- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    //实时改变子选项菜单
    [self.subDataArray removeAllObjects];
    [self.subDataArray addObject:[NSString stringWithFormat:@"全部%@",selectTitle]];
    self.subClassfiy = nil;
    [self addSubview:self.subClassfiy];

}

#pragma mark -- getters

- (ButtonAndTableView *)subClassfiy{
    if (!_subClassfiy) {
        _subClassfiy = ({
            ButtonAndTableView *subClassfiy = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(frameWidth * 0.6, 0, frameWidth * 0.35, frameHeight* 0.05) dataSource:@{@"title":self.subDataArray[0]} backImageName:@"理财_下拉框" selectImageName:@"理财_下拉框"];
            subClassfiy;
        });
    }
    return _subClassfiy;
}





- (NSMutableArray *)subDataArray{
    if (!_subDataArray) {
        _subDataArray = [NSMutableArray array];
        [_subDataArray addObject:@"全部年金险"];
    }
    return _subDataArray;
}



@end
